<?php

class Dictionary {
	//Database info
	protected $hostname = 'localhost';
	protected $username = 'smurfer';
	protected $password = 'refrums';
	protected $database = 'dcln';
	protected $table	= 'dicln';

	/**
	 *	Dictionary constructor
	 */
	function __construct() {
		mysql_pconnect($this->hostname, $this->username, $this->password);
		mysql_select_db($this->database);
	}
	
	/**
	 *	Add a word
	 */
	function addWord($word, $definition) {
		//TODO: write code to add a word to the database
	}
	
	/**
	 *	Delete a word
	 */
	function deleteWord($word) {
		//TODO: write code to delete a word from the database
	}
	
	/**
	 *	Update a word
	 */
	function updateWord($word, $definition) {
		//TODO: write code to update a word in the database
	}
	
	/**
	 *	List all words
	 */
	function listWords() {
		$query = "select word, definition from $this->table order by word asc";
		$result = mysql_query($query) or die(mysql_error());
		
		$indexLetter = '';
		while ($row = mysql_fetch_array($result)) {
			$word = $row['word'];
			$definition = $row['definition'];
			$firstLetter = strtoupper(substr($word, 0, 1));
			if ($indexLetter != $firstLetter) {
				$indexLetter = $firstLetter;
				echo "<br/><strong>$indexLetter</strong><br/>";
			}
			echo "&nbsp;<strong>$word</strong> - $definition<br/>";
		}
	}

}